package cn.tianyigps.tianyigpsguazi.customer;

import cn.tianyigps.tianyigpsguazi.handler.TaskHandler;
import cn.tianyigps.tianyigpskafka.task.bean.Task;
import cn.tianyigps.tianyigpspublic.util.FormatUtil;
import org.joda.time.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import cn.tianyigps.tianyigpskafka.task.customer.Customer;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;
import java.util.Optional;

/**
 * cn.tianyigps.tianyigpsguazi.customer
 *
 * @author zgl
 * @create 2019-03-22 15:25
 */
@Component
public class GuaziCustomer extends  Customer{
    @Autowired
    private TaskHandler taskHandler;

    @Override
    public boolean checkTask(Task task) {

        Map<String,Object> params =  task.getParams();
        if(!Optional.ofNullable(params).isPresent()){
            return  false;
        }

        Task nextTask = task.getNextTask();
        if(!Optional.ofNullable(nextTask).isPresent()){
            return  false;
        }

        String equipmentId = FormatUtil.format2String(params.get("equipmentId"));
        if(StringUtils.isEmpty(equipmentId)){
            return  false;
        }

        Date start = FormatUtil.format2Date(params.get("start"));
        if (!Optional.ofNullable(start).isPresent()) {
            return false;
        }
        Date end = FormatUtil.format2Date(params.get("end"));
        if (!Optional.ofNullable(end).isPresent()) {
            return false;
        }

        return true;
    }

    @Override
    public void consume(Task task) {
        taskHandler.consume(task);
    }

}
